package itsm.isperp.framework.solr.parser;

import org.apache.lucene.search.Query;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.search.QParser;
import org.apache.solr.search.QueryParsing;
import org.apache.solr.search.SolrQueryParser;
import org.apache.solr.search.SyntaxError;

/**
 * 扩展到分词解析器
 * 
 * @author lizx
 * 
 */
public class ExtLuceneQParser extends QParser {

	private SolrQueryParser lparser;

	public ExtLuceneQParser(String qstr, SolrParams localParams,
			SolrParams params, SolrQueryRequest req) {
		super(qstr, localParams, params, req);
	}

	// ===============copy from org.apache.solr.search.LuceneQParser
	@Override
	public Query parse() throws SyntaxError {
		String qstr = getString();
		if (qstr == null || qstr.length() == 0)
			return null;

		String defaultField = getParam(CommonParams.DF);
		if (defaultField == null) {
			defaultField = getReq().getSchema().getDefaultSearchFieldName();
		}
		lparser = new ExtSolrQueryParser(this, defaultField);

		lparser.setDefaultOperator(QueryParsing.getQueryParserDefaultOperator(
				getReq().getSchema(), getParam(QueryParsing.OP)));

		Query q = lparser.parse(qstr);

		// Query q2 = ((ExtSolrQueryParser) lparser).getFieldQuery(defaultField,
		// qstr, 1);
		return q;
	}

	@Override 
	public String[] getDefaultHighlightFields() {
		return lparser == null ? new String[] {} : new String[] { lparser
				.getDefaultField() };
	}
	// ================end=======================//
}
